<?php

/**
 * 新客户留言接口
 * 用于来自 Q1  M站  微快递  www主站 的客户留言
 *
 * message 库  tbl_message表
 *
 * add by ljp 2015-10-22
 */

namespace soa\liuyan;

use soa\client;

class message extends client
{

    public $service_url = 'http://message.core.kuaidihelp.com/';
    protected $service_name = 'kuaidihelp_message';

    public function getList($where = array(), $page = 1, $page_size = 20, $fields = '*')
    {
        return $this->get('v1/liuyan/getLiuyanList', array_merge($where,array('page_num'=> $page, 'page_size' => $page_size), ['fields' => $fields]));
    }

    public function getInfo($message_id){
        return $this->get('v1/liuyan/getLiuyan', array('message_id' => $message_id));
    }

    public function getInfoByFeedbackID($feedback_id){
        return $this->get('v1/liuyan/getLiuyanByFeedbackID', array('feedback_id' => $feedback_id));
    }

    public function getReplys($where, $fields = '*'){
        return $this->get('v1/liuyan/getReplyList', array_merge($where, ['fields' => $fields]));
    }


    /**
     * 添加新留言
     *
     * @param $exp_no
     *
     * @param $poster
     * array(
     *      'poster_id'     创建者id,
     *      'poster_role'   创建者身份,
     *      'poster_name'   创建者名字,
     *      'poster_phone'  创建者手机
     *  );
     *
     *
     * @param $shop
     * array(
     *      'shop_id'                   网点 index_shop_id
     *      'shop_name'                 网点  index_shop_name,
     *      'shop_brand'                网点品牌,
     *      'shop_counterman_id'        快递员id,
     *      'shop_counterman_name'      快递员姓名,
     *      'shop_counterman_mobile'    快递员手机
     *  );
     *
     * @param $message
     * array(
     *      'content_type'  消息类型,
     *      "content"       消息内容,
     *      "voice_length"  音频长度,
     *      "msg_name" ,
     *      "msg_mobile"  ,
     *      "msg_address"  ,
     *  )
     *
     * @param int $push_to_mcg 是否推送到留言系统
     * @param string $source
     * @param string $org_code
     * @param string $topic_id
     * @param string $open_sign
     * @param int $push_to_device
     * @return bool
     * @internal param int $app_id 来源
     */
    public function addMessage($exp_no,$poster,$shop,$message,$push_to_mcg = 0,$source = '',$org_code = '',$topic_id='',$open_sign='',$push_to_device = 'on'){

        $data = array(
            'exp_no' => $exp_no,
            'org_code'=>$org_code,
            'poster' => $poster,
            'shop' => $shop,
            'message' => $message,
            'push_to_mcg' => $push_to_mcg,
            "source" => $source,
            'topic_id'=>$topic_id,
            'open_sign'=> $open_sign,
            'push_to_device' => $push_to_device
        );
        return $this->post('v1/liuyan/addLiuyan', $data);
    }

    //因为q1发留言有时候会在入库时候失败  导致库里没有数据 但留言系统又存在
    //所以特地做了这个做修复用
    //add by ljp
    public function fixMessage($feedback_id,$exp_no,$poster,$shop,$message,$post_time = '',$source = ''){

        $data = array(
            'feedback_id' => $feedback_id,
            'exp_no' => $exp_no,
            'poster' => $poster,
            'shop' => $shop,
            'message' => $message,
            'push_to_mcg' => 0,
            'post_time' => $post_time,
            "source" => $source
        );

        return $this->post('v1/liuyan/addLiuyan', $data);
    }

    /**
     * 添加录音记录
     *
     * @param $exp_no
     * @param $poster
     * @param $user
     * @param $message
     * @param int $app_id
     * @return bool
     */
    public function addRecord($exp_no,$poster,$user,$message,$source = ''){

        $data = array(
            'exp_no' => $exp_no,
            'poster' => $poster,
            'user' => $user,
            'message' => $message,
            "source" => $source
        );

        return $this->post('v1/liuyan/addRecord', $data);
    }

    /**
     * 添加回复
     *
     * @param $message_id
     * @param $poster
     *  array(
     *      'poster_id'     创建者id,
     *      'poster_role'   创建者身份,
     *      'poster_name'   创建者名字,
     *      'poster_phone'  创建者手机
     *  );
     *
     * @param $message
     * array(
     *      'content_type'  消息类型,
     *      "content"       消息内容,
     *      "voice_length"  音频长度,
     *      "msg_name" ,
     *      "msg_mobile"  ,
     *      "msg_address"  ,
     *  )
     *
     * @param string $source
     * @param string $push_to_device
     * @return bool
     * @internal param int $app_id
     */

    public function addReply($message_id,$poster,$message,$source = '', $push_to_device = 'on'){
        $data = array(
            "message_id" => $message_id,
            "poster" => $poster,
            "message" => $message,
            "source" => $source,
            'push_to_device' => $push_to_device
        );
        return $this->post('v1/liuyan/addReply', $data);
    }


    /**
     * 用于来自留言系统的回传
     *
     * @param $feedback_id
     * @param $poster
     * @param $message
     * @param int $app_id
     * @return bool
     */
    public function addReplyByFeedbackID($feedback_id,$poster,$message,$post_time = '',$push_sms = 0, $source = ''){
        $data = array(
            "feedback_id" => $feedback_id,
            "poster" => $poster,
            "message" => $message,
            "source" => $source,
            'post_time' => $post_time,
            'push_to_mcg' => 0,
            'push_sms' => $push_sms
        );
        return $this->post('v1/liuyan/addReplyCross', $data);
    }

    public function unreadstat($role,$id,$start_time='',$end_time=''){
        $data = array(
            "poster_role" => $role,
            'poster_id' => $id,
            'start_time'=>$start_time,
            'end_time'=>$end_time
        );
        return $this->post('v1/liuyan/unReadCount', $data);
    }

    public function readedstat($role,$id,$start_time='',$end_time=''){
        $data = array(
            "user_role" => $role,
            'user_id' => $id,
            'start_time'=>$start_time,
            'end_time'=>$end_time
        );
        return $this->post('v1/liuyan/readedCount', $data);
    }

    public function getFirstReplys($message_ids){
        return $this->post('v1/liuyan/getFirstReplys', array('message_ids' => $message_ids ));
    }


    /**
     * 分配留言
     * 用于来自留言系统的分配操作， 讲留言分配给某个快递员，
     * 由于自身业务没有分配功能， 所以这里只是简单的更新操作，但涉及参数还是为将来做准备
     *
     * @param $feedback_id
     * @param $courier  要分配的快递员信息
     *  array(
     *      'uid'        快递员id （cm_id）,
     *      'userno'     快递员工号,
     *      'username'   快递员名字
     *  );
     *
     *
     * @param $operation  操作人信息
     *  array(
     *      'org_code'   操作网点code
     *      'org_name'   操作网点名称,
     *      'userno'     操作人工号,
     *      'username'   操作人姓名
     *  );
     *
     *
     * @param string $source
     * @return bool
     */
    public function assignMessage($feedback_id,$courier,$operation,$source = ''){
        $data = array(
            "feedback_id" => $feedback_id,
            'courier' => $courier,
            'operation' => $operation,
            "source" => $source
        );
        return $this->post('v1/liuyan/assignLiuyan', $data);
    }

    //改变TOPIC状态
    public function changeMessageStatus($message_id,$status){
        return $this->post('v1/liuyan/changeLiuyanStatus', array('message_id' => $message_id,'status'=>$status));
    }
    //更新数据接口
    public function updateMessage($message_id,$data,$push_to_app = false){
        return $this->post('v1/liuyan/updateMessage',array('message_id'=>$message_id,'data'=>$data,'push_to_app'=>$push_to_app));
    }

    //消息推送
    public function push_message_to($user_type,$user_id,$topic_id,$topic_type,$title,$content){
        $data = array(
            "user_type" => $user_type,
            'user_id' => $user_id,
            'topic_id' => $topic_id,
            'topic_type' => $topic_type,
            "title" => $title,
            "content" => $content
        );
        return $this->post('v1/liuyan/push_to', $data);
    }


    /******************  collection 类所需  ********************/

    public function topic_list_request($data){
		//logDebug($data,'3292');
        return $this->_request_api_url('v1/liuyan/getLiuyanList',$data);
    }

    public function reply_list_request($data){
        return $this->_request_api_url('v1/liuyan/getReplyList',$data);
    }

    public function unread_count_request($data){
        return $this->_request_api_url('v1/liuyan/unReadCount',$data);
    }

    private function _request_api_url($api,$data = array()){
        $data = $this->request_params($api, $data);
        return $this->request_api($api).'?'.http_build_query($data);
    }
}